﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading.Tasks;
using System.Net.Sockets;
namespace NetWorkTest
{
    class TCPClient
    {
        static int SERVER_PORT = 9088;
        IPAddress SERVER_IP;// = IPAddress.Parse("192.168.1.11");
        TcpClient client;
         NetworkStream ns;
         //   Connect(SERVER_HOST, SERVER_PORT);
       
        byte[] rcvbuf = new byte[1024];
        public int StartConnect(string ip){
            client = new TcpClient();
            SERVER_IP = IPAddress.Parse(ip);
            client.Connect(SERVER_IP, SERVER_PORT);
            ns = client.GetStream();
            return 0;
        }
        public int getTcpData(ref byte[]rcvbuf){
            try {
                return ns.Read(rcvbuf, 0, 1024);
            }
            catch (Exception err)
            {
                return -1;
            }
        }
        public void sendTcpData(byte[] data,int len)
        {
            //byte[] by = Encoding.UTF8.GetBytes(data);
            ns.Write(data, 0, len);
        }
        public int StartClose()
        {
            byte[] by = {1,2};
            //ns.Close();
           // ns.Flush();
            ns.Dispose();
            //client.();
            client.Close();
            //client.Dispose(false);
          //  ((IDisposable)client).Dispose();
           // ns.Write(by, 0, 2);
            client = null;
            

            return 0;
        }

    }
}
